// pages/transSubOrder/transSubOrder.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0, //订单id
    orderInfo: null,
    baseImgPath: app.globalData.baseImgPath,
    backImgPath: app.globalData.URL,
    imgList: [],
    canSub: true,
    orderInfo: {},
    ffetchtext: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this;
    that.setData({
      id: options.id //登陆页面取回来的id
    });
    wx.getStorage({
      key: 'sessionId',
      success: function(res) {
        that.setData({
          sessionId: res.data, //登陆页面取回来的sessionId
        });
        console.log(that.data.sessionId);
        wx.request({
          url: app.globalData.urlPath + '/ManagerMent/expressInfo',
          data: {
            sessionid: that.data.sessionId,
            id: that.data.id
          },
          method: "POST",
          success: function(res) {
            console.log(res)
            var data = res.data.data;
            that.setData({
              orderInfo: data
            })
          }
        })
      },
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //上传图片
  choose: function () { //这里是选取图片的方法
    var that = this;
    var pics = [];
    wx.chooseImage({
      count: 9 - pics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        console.log(res);
        var imgsrc = res.tempFilePaths;
        pics = that.data.imgList.concat(imgsrc);
        that.setData({
          pics: pics
        });

        that.uploadimg(imgsrc);
        //if (app.globalData.successN == imgsrc.length){
        // that.setData({
        //   imgList: app.globalData.pic
        // })
        that.setData({
          imgList: pics
        })
        console.log(that.data.pics)
        //}
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },
  uploadimg: function (imgsrc) { //这里触发图片上传的方法
    var that = this;
    var pics = this.data.pics;
    console.log(pics)
    app.uploadimg1({
      url: app.globalData.urlPath + 'ManagerMent/uploadpic', //这里是你图片上传的接口
      path: imgsrc //这里是选取的图片的地址数组
    });
  },
  chooseImg: function() {
    let self = this;
    if (this.data.imgList.length < 9) {
      wx.chooseImage({
        count: 1,
        sourceType: ['album', 'camera'],
        success: function(res) {

          const tempFilePaths = res.tempFilePaths;
          wx.uploadFile({
            url: app.globalData.urlPath + 'ManagerMent/uploadpic',
            filePath: tempFilePaths[0],
            header: { "Content-Type": "multipart/form-data" },
            name: 'file',
            formData: {
              sessionid: self.data.sessionId
            },
            success: function (res) {
              console.log(res.data)
              let imgdata = JSON.parse(res.data)
              if (imgdata.code == 200) {
                let imgPath = imgdata.data;
                self.setData({ imgList: self.data.imgList.concat(imgPath) })
              } else {
                wx.showToast({
                  title: '上传失败，请稍后重新',
                  icon: 'none'
                })
              }

            },
            fail: function () {
              wx.showToast({
                title: '上传失败，请稍后重新',
                icon: 'none'
              })
            }
          })


          // wx.getFileSystemManager().readFile({
          //   filePath: tempFilePaths[0], //选择图片返回的相对路径
          //   encoding: 'base64', //编码格式
          //   success: res => { //成功的回调
          //     //console.log('data:image/png;base64,' + res.data)
          //     let file = res.data;
          //     self.setData({
          //       // url: 'data:image/jpg;base64,' + base64
          //       url: file
          //     })
          //          wx.showToast({
          //       title: '正在上传...',
          //       icon: 'loading',
          //       mask: true,
          //       //  duration: 5000,
          //       success: function() {
          //         //请求接口
          //         wx.request({
          //           method: "POST",
          //           url: app.globalData.urlPath + 'ManagerMent/base64',
          //           data: {
          //             base64: self.data.url,
          //             sessionid: self.data.sessionId
          //           },
          //           success(res) {
          //             var imgUrl = res.data.data;
          //             //do something

          //             self.setData({
          //               imgList: self.data.imgList.concat(imgUrl)
          //             })
          //           }
          //         })
          //       }
          //     })
          //   }
          // })
        },
      })
    }
  },
  //删除上传的图片
  deletePic: function (e) {
    console.log(e)
    var self = this;
    wx.showToast({
      title: '删除成功！',
    })
    var imgs = self.data.imgList;
    var pics = app.globalData.pic;
    //console.log(pics)
    console.log(app.globalData.pic)
    var index = e.currentTarget.dataset.index;
    console.log(index)
    pics.splice(index, 1);
    imgs.splice(index, 1);

    self.setData({
      imgList: imgs
    });
    console.log(self.data.imgList)
    console.log(app.globalData.pic)

    wx.setStorage({
      key: 'imgPic',
      data: self.data.imgList,
    })
    // app.globalData.pic = self.data.imgList;
  },
  //底部导航切换
  handleChangeTab({ detail }) {
    console.log(detail.key)
    if (detail.key == 'order') {
      wx.reLaunch({
        url: '/pages/transGetList/transGetList',
      })
    } else if (detail.key == 'code') {
      // 只允许从相机扫码
      wx.scanCode({
        onlyFromCamera: true,
        success(res) {
          console.log(res);
          that.setData({
            qrcode: res.result
          })
          wx.showModal({
            title: '是否确认收货?',
            // content: '模态弹窗',
            success: function (res) {
              if (res.confirm) {
                //加载二维码返回数据
                wx.request({
                  url: app.globalData.urlPath + '/ManagerMent/scanqrcode',
                  data: {
                    sessionid: app.globalData.sessionid,
                    qrcode: that.data.qrcode
                  },
                  method: "POST",
                  success: function (res) {
                    console.log(res)
                    if (res.data.code == 200) {
                      wx.showToast({
                        title: res.data.data,
                        icon: 'success',
                        duration: 2000
                      })
                      setTimeout(function(){
                        wx.redirectTo({
                          url: '/pages/transOrderList/transOrderList?current=5',
                        })
                      })
                    }
                    else {
                      wx.showToast({
                        title: res.data.error,
                        icon: 'none',
                        duration: 2000
                      })
                      if (res.data.code == 5000) {
                        wx.redirectTo({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  }
                })
              } else {
                return;
              }

            }
          })


        }
      })
    } else if (detail.key == 'my') {
      wx.redirectTo({
        url: '/pages/transMy/transMy',
      })
    }
    this.setData({
      currentTab: detail.key
    });
  },
  //提交订单
  formSubmit: function(e) {
    var that = this;
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/expressTak',
      method: 'POST',
      data: {
        sessionid: that.data.sessionId, //sessionId
        id: that.data.id, //订单id
        ffetchpic: app.globalData.pic, //图片列表
        ffetchtext: e.detail.value.textarea,//文本框
      },
      success: function(res) {
        console.log(res);
        if(res.data.code==200){
          wx.showToast({
            title: res.data.data,
            icon: 'success',
            duration: 2000
          });
          app.globalData.pic = [];
         setTimeout(function(){
           wx.redirectTo({
             url: '/pages/transOrderList/transOrderList',
           })
         },2000)}
         else{
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          });
          if (res.data.code == 5000) {
             wx.redirectTo({
              url: '/pages/login/login',
            })
          }
          that.setData({
            list: ''
          });
        }
      }
    })
  }
})